package com.eshidai.simple_springboot.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import com.eshidai.simple_springboot.bean.Email;
import com.eshidai.simple_springboot.bean.MsgSend;
import com.eshidai.simple_springboot.common.EmailMsgSend;
import com.eshidai.simple_springboot.service.HelloService;
import com.eshidai.simple_springboot.service.MsgSendService;

@Service
public class MsgSendServiceImpl implements MsgSendService {
	@Autowired
    private JdbcTemplate jdbcTemplate;
	
	@Autowired
	private EmailMsgSend emailMsgSend; 
	
	@Autowired
	private HelloService helloService;

	public void create(MsgSend msg) {
		if(msg != null) {
			jdbcTemplate.update("insert into msg_send_jrn (msg_id,msg_type,sender,send_time) values(?,?,?,?)",
					msg.getMsgId(),msg.getMsgType(),msg.getSender(),msg.getSendTime());
			
			try {
				helloService.addUser();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public void deleteByMsgId(String msgId) {
		jdbcTemplate.update("delete from msg_send_jrn where msg_id='"+msgId+"'");
	}

	public List<MsgSend> getMsgSendList() {
		String sql = "select msg_id,msg_type,sender,send_time from msg_send_jrn";
		
		return null;
	}
	
	@Async
	public void updateByMsgId(String msgId) {
		jdbcTemplate.update("update msg_send_jrn set msg_type=2 where msg_id='"+msgId+"'");
	}

	public void sendSimpleMail(Email email) {
		emailMsgSend.sendSimpleMail(email);
	}

}
